const usersModel = require("../model/usersModel");

//查询所有用户
module.exports.find = async function (usersObj, page) {
  let { current, size } = page;
  let pagesize = await usersModel.countDocuments(usersObj);
  let pageNum = Math.ceil(pagesize / size);

  let pagedata = await usersModel
    .find(usersObj)
    .limit(parseInt(size))
    .skip((current - 1) * size);

  return {
    current,
    size,
    pagesize,
    pageNum,
    pagedata,
  };
};

//根据 ID 查询
module.exports.findById = async function (id) {
  return await usersModel.findById({ _id: id });
};

// 增加用户
module.exports.insert = async function (usersObj) {
  return await usersModel.create(usersObj);
};
//  查询用户是否存在
module.exports.findOneId = async function (username) {
  let data = await usersModel.findOne({ username: username });
  if (data == null) {
    return true;
  }else{
    return data;
  }
};
//修改用户
module.exports.update = async function (id, user) {
  return await usersModel.updateOne({ _id: id }, user);
};
// 根据 ID 删除用户
module.exports.delete = async function (id) {
  return await usersModel.deleteOne({ _id: id });
};
